//---------------------Sonic CD Start Script------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
#alias Object.Value0	:	Object.Fade
#alias Object.Value1	:	Object.StageSelectCheatPos
#alias Object.Value2	:	Object.SoundTestCheatPos
#alias Object.Value3 	:	Object.DemoTimer

// States
#alias 0	:	START_SETUP
#alias 1	:	START_GOTO_MENU
#alias 2	:	START_GOTO_STAGESELECT // Unused
#alias 3	:	START_GOTO_SOUNDTEST // Unused
#alias 4	:	START_GOTO_DEMO

// Cheat Input Aliases
#alias 0	:	CHEATINPUT_NONE
#alias 1	:	CHEATINPUT_UP
#alias 2	:	CHEATINPUT_DOWN
#alias 3	:	CHEATINPUT_LEFT
#alias 4	:	CHEATINPUT_RIGHT
#alias 5	:	CHEATINPUT_BUTTONA
#alias 6	:	CHEATINPUT_BUTTONB
#alias 7	:	CHEATINPUT_BUTTONC

// DemoNumber Aliases
#alias 0	:	DEMO_R11A
#alias 1	:	DEMO_SS1
#alias 2	:	DEMO_R43C
#alias 3	:	DEMO_SS6
#alias 4	:	DEMO_R82A
#alias 5	:	DEMO_MAX

// Select Aliases
#alias 0	:	SELECT_INIT

// Global SFX
#alias 1	:	SFX_G_RING
#alias 27	:	SFX_G_SELECT

// Callbacks & Engine States
#alias 1	:	CALLBACK_PRESS_START
#alias 7	:	CALLBACK_EXIT_SELECTED
#alias 16	:	CALLBACK_MOREGAMES

// Game Mode Aliases
#alias 0	:	MODE_NOSAVE

// Language Aliases
#alias 0	:	LANG_ENGLISH
#alias 1	:	LANG_FRENCH
#alias 2	:	LANG_ITALIAN
#alias 3	:	LANG_DEUTSCH
#alias 4	:	LANG_SPANISH
#alias 5	:	LANG_JAPANESE

// Presentation Stages
#alias 1	:	STAGE_P_MENU
#alias 3	:	STAGE_P_STAGESELECT
#alias 4	:	STAGE_P_SOUNDTEST

// Regular Stages
#alias 0	:	STAGE_R_R11A
#alias 38 	:	STAGE_R_R43C
#alias 84	:	STAGE_R_R82A

// Special Stages
#alias 0	:	STAGE_S_SS1
#alias 5	:	STAGE_S_SS6

// Device Type
#alias 0	:	RETRO_STANDARD

// Entirely functional, yet unused
// Perhaps it was removed because it would devalue unlocking things in Time Attack?
// No way to say for sure
#function TouchStart_HandleCheatCodes


function TouchStart_HandleCheatCodes
	TempValue0 = CHEATINPUT_NONE
	
	if KeyPress[0].Up == true
		TempValue0 = CHEATINPUT_UP
	end if
	
	if KeyPress[0].Down == true
		TempValue0 = CHEATINPUT_DOWN
	end if
	
	if KeyPress[0].Left == true
		TempValue0 = CHEATINPUT_LEFT
	end if
	
	if KeyPress[0].Right == true
		TempValue0 = CHEATINPUT_RIGHT
	end if
	
	if KeyPress[0].ButtonA == true
		TempValue0 = CHEATINPUT_BUTTONA
	end if
	
	if KeyPress[0].ButtonB == true
		TempValue0 = CHEATINPUT_BUTTONB
	end if
	
	if KeyPress[0].ButtonC == true
		TempValue0 = CHEATINPUT_BUTTONC
	end if
	
	// Up, Down, Down, Left, Right, B
	switch Object.StageSelectCheatPos
	case 0
		if TempValue0 == CHEATINPUT_UP
			Object.StageSelectCheatPos++
		else
			if TempValue0 != CHEATINPUT_NONE
				Object.StageSelectCheatPos = 0
			end if
		end if
		break
		
	case 1
		if TempValue0 == CHEATINPUT_DOWN
			Object.StageSelectCheatPos++
		else
			if TempValue0 != CHEATINPUT_NONE
				Object.StageSelectCheatPos = 0
			end if
		end if
		break
		
	case 2
		if TempValue0 == CHEATINPUT_DOWN
			Object.StageSelectCheatPos++
		else
			if TempValue0 != CHEATINPUT_NONE
				Object.StageSelectCheatPos = 0
			end if
		end if
		break
		
	case 3
		if TempValue0 == CHEATINPUT_LEFT
			Object.StageSelectCheatPos++
		else
			if TempValue0 != CHEATINPUT_NONE
				Object.StageSelectCheatPos = 0
			end if
		end if
		break
		
	case 4
		if TempValue0 == CHEATINPUT_RIGHT
			Object.StageSelectCheatPos++
		else
			if TempValue0 != CHEATINPUT_NONE
				Object.StageSelectCheatPos = 0
			end if
		end if
		break
		
	case 5
		if TempValue0 == CHEATINPUT_BUTTONB
			Object.StageSelectCheatPos = 0
			
			// Go to the Stage Select
			Object.State = START_GOTO_STAGESELECT
			StopMusic()
			
			// Just a cool lil' jingle :)
			PlaySfx(SFX_G_RING, false)
		else
			if TempValue0 != CHEATINPUT_NONE
				Object.StageSelectCheatPos = 0
			end if
		end if
		break
	end switch
	
	// Down, Down, Down, Left, Right, A
	switch Object.SoundTestCheatPos
	case 0
		if TempValue0 == CHEATINPUT_DOWN
			Object.SoundTestCheatPos++
		else
			if TempValue0 != CHEATINPUT_NONE
				Object.SoundTestCheatPos = 0
			end if
		end if
		break
		
	case 1
		if TempValue0 == CHEATINPUT_DOWN
			Object.SoundTestCheatPos++
		else
			if TempValue0 != CHEATINPUT_NONE
				Object.SoundTestCheatPos = 0
			end if
		end if
		break
		
	case 2
		if TempValue0 == CHEATINPUT_DOWN
			Object.SoundTestCheatPos++
		else
			if TempValue0 != CHEATINPUT_NONE
				Object.SoundTestCheatPos = 0
			end if
		end if
		break
		
	case 3
		if TempValue0 == CHEATINPUT_LEFT
			Object.SoundTestCheatPos++
		else
			if TempValue0 != CHEATINPUT_NONE
				Object.SoundTestCheatPos = 0
			end if
		end if
		break
		
	case 4
		if TempValue0 == CHEATINPUT_RIGHT
			Object.SoundTestCheatPos++
		else
			if TempValue0 != CHEATINPUT_NONE
				Object.SoundTestCheatPos = 0
			end if
		end if
		break
		
	case 5
		if TempValue0 == CHEATINPUT_BUTTONA
			Object.SoundTestCheatPos = 0
			
			// Go to the Sound Test
			Object.State = START_GOTO_SOUNDTEST
			StopMusic()
			
			PlaySfx(SFX_G_RING, false)
		else
			if TempValue0 != CHEATINPUT_NONE
				Object.SoundTestCheatPos = 0
			end if
		end if
		break
	end switch
end function


sub ObjectMain
#platform: Use_Origins
	Menu.ExitFlag = true
#endplatform
	
	switch Object.State
	case START_SETUP
		CheckTouchRect(0, 0, Screen.XSize, 240)
		
#platform: Use_Origins
		// On Origins, you can back out if you really want to
		if KeyPress[0].ButtonB == true
			EngineCallback(NOTIFY_BACK_TO_MAINMENU)
		end if
		
		if KeyPress[0].Start == true
			CheckResult = false
		end if
		
		if KeyPress[0].ButtonA == true
			CheckResult = false
		end if

		if input.pressButton == true
			CheckResult = false
		end if
#endplatform

#platform: Use_Standalone
		if KeyPress[0].Start == true
			CheckResult = false
		end if
#endplatform

		if CheckResult > -1	// Check if you used the touch input, or if what's above is true
			// The Player's (reportedly) interacted with the game in some form, so start the Select sequence
			Object.State = START_GOTO_MENU	// Select Menu
		
#platform: Use_Origins
			Object[+1].Type  = TypeName[Select]
			Object[+1].iXPos = Screen.CenterX
			Object[+1].iYPos = 200
			Object[+1].State = SELECT_INIT
#endplatform

#platform: Use_Standalone
			StopMusic()
			PlaySfx(SFX_G_SELECT, false)
			
#endplatform

#platform: Use_Haptics
			HapticEffect(31, 0, 0, 0)
#endplatform
		else
			Object.DemoTimer++
			
			if Object.DemoTimer == 1550
				Object.State = START_GOTO_DEMO
			end if
		end if
		
		// I mean, if you want to I guess?
		if Engine.PlatformID == RETRO_WP7
			if KeyPress[0].ButtonB == true
				EngineCallback(CALLBACK_EXIT_SELECTED)
			end if
		end if
		break
		
	case START_GOTO_MENU
		// In Origins, this state does nothing and all the stuff in handled by the Select object instead
#platform:	Use_Standalone
		Object.Fade += 4
		if Object.Fade == 384
			Stage.ListPos = STAGE_P_MENU
			LoadStage()
		end if
		
		SetScreenFade(0, 0, 0, Object.Fade)
#endplatform
		break
		
	case START_GOTO_STAGESELECT // This state is unused
		Object.Fade += 4
		
		if Object.Fade == 384
			Options.GameMode = MODE_NOSAVE
			Stage.ListPos = STAGE_P_STAGESELECT
			LoadStage()
		end if
		
		SetScreenFade(0, 0, 0, Object.Fade)
		break
		
	case START_GOTO_SOUNDTEST // This state is also unused
		Object.Fade += 4
		
		if Object.Fade == 384
			Options.GameMode = MODE_NOSAVE
			Stage.ListPos = STAGE_P_SOUNDTEST
			LoadStage()
		end if
		
		SetScreenFade(0, 0, 0, Object.Fade)
		break
		
	case START_GOTO_DEMO
		Object.Fade += 4
		
		if Object.Fade == 384
			Engine.HapticsEnabled = false
			
			Fade_Colour = 0x000000
			
			// Reset loads of stuff
			Player.Score	  		= 0
			Player.Lives	  		= 3
			Player.ScoreBonus		= 50000
			Transporter_Destroyed 	= false
			MetalSonic_Destroyed 	= false
			Good_Future 			= false
			Options.GameMode		= MODE_NOSAVE
			Options.AttractMode 	= true

			if Engine.PlatformID == RETRO_ANDROID
				if Engine.TrialMode == true
					Options.DemoNumber = 0
				end if
			end if

			switch Options.DemoNumber
			case DEMO_R11A
				// R11A - Palmtree Panic Act 1, Present
				Stage.ActiveList = REGULAR_STAGE
				Stage.ListPos	 = STAGE_R_R11A
				break
				
			case DEMO_SS1
				// SS1 - Special Stage 1
				Stage.ActiveList = SPECIAL_STAGE
#platform: Use_Origins
				Stage.ActiveList = 3
#endplatform
				Stage.ListPos	 = STAGE_S_SS1
				break
				
			case DEMO_R43C
				// R43C - Tidal Tempest 3, Good Future
				Stage.ActiveList = REGULAR_STAGE
				Stage.ListPos	 = STAGE_R_R43C
				break
				
			case DEMO_SS6
				// SS6 - Special Stage 6
				Stage.ActiveList = SPECIAL_STAGE
#platform: Use_Origins
				Stage.ActiveList = 3
#endplatform
				Stage.ListPos	 = STAGE_S_SS6
				break
				
			case DEMO_R82A
				// R82A - Metallic Madness 2 Present
				Stage.ActiveList = REGULAR_STAGE
				Stage.ListPos	 = STAGE_R_R82A
				break
			end switch
			
			// Cycle through the same 6 demos
			Options.DemoNumber++
			Options.DemoNumber %= 5

#platform: Use_Origins
			// Sonic Team REALLY hates the concept of Amy being in attract demos
			// The Sonic object already sets the player to him, so this is pretty redundant
			if Stage.PlayerListPos == PLAYER_AMY
				Stage.PlayerListPos = PLAYER_SONIC
			end if
#endplatform

			LoadStage()
		end if
		
		SetScreenFade(0, 0, 0, Object.Fade)
		break
		
	end switch
	
end sub


sub ObjectDraw
#platform: Use_Origins
	// Don't display the following if the Select menu is open
	if Object.State != START_GOTO_MENU
		Object.AnimationTimer++
		if Object.AnimationTimer > 59
			Object.AnimationTimer = 0
		end if
		
		// If in the upper half of a second, then
		if Object.AnimationTimer > 29
			if Engine.DeviceType == RETRO_STANDARD
				// On standard Platforms, display the "Press Button" text
				DrawSprite(2)
			else
				// On mobile Platforms, display the corresponding language text for
				// telling the player to tap the screen
				DrawSprite(1)
			end if
		end if
	end if
	
	// Get the cordinates for the bottom right of the screen
	TempValue0  = Screen.XSize
	TempValue0 -= 22
	
	TempValue1  = Screen.YSize
	TempValue1 -= 12
	
	// Draw the Sega copyright sprite there
	DrawSpriteScreenXY(3, TempValue0, TempValue1)
#endplatform

#platform: Use_Standalone
	Object.AnimationTimer++
	if Object.AnimationTimer > 59
		Object.AnimationTimer = 0
	end if
#endplatform

#platform: Use_Standalone
	#platform: Standard
		if Object.AnimationTimer > 29
			DrawSprite(0)
		end if
	#endplatform
#endplatform

#platform: Use_Standalone
	#platform: Mobile
		if Object.AnimationTimer > 29
			DrawSprite(1)
		end if
	#endplatform
#endplatform
end sub


sub ObjectStartup
#platform: Use_Origins
	game.callbackParam0 = false
	EngineCallback(NOTIFY_LEVEL_SELECT_MENU)
#endplatform
	LoadSpriteSheet("Title/Title.gif")

	// The following Sprites differ between languages
	
	// They follow the order of
	// 0 - Press to Start
	// 1 - Touch to Start!
	
	// But of course in the language they correspond to
	
	switch Engine.Language
	case LANG_ENGLISH
	case LANG_JAPANESE
		SpriteFrame(-43, 0,  87, 11, 106, 103) // Press Start
		SpriteFrame(-56, 0, 117, 11, 106,  91) // Touch to Start!
		break
		
	case LANG_FRENCH
		SpriteFrame(-60, 0, 120, 11, 391, 387) // Appui sur Start
		SpriteFrame(-94, 0, 189, 11, 322, 399) // Touche pour Commencer !
		break
		
	case LANG_ITALIAN
		SpriteFrame(-44, 0,  88, 11, 270, 411) // Premi Start
		SpriteFrame(-76, 0, 152, 11, 359, 411) // Tocca Per Iniziare!
		break
		
	case LANG_DEUTSCH
		SpriteFrame(-52, -2, 106, 13, 405, 373) // Start Drücken
		SpriteFrame(-87,  0, 175, 11, 336, 423) // Zum Starten Antippen!
		break
		
	case LANG_SPANISH
		SpriteFrame(-43, 0,  87, 11, 274, 435) // Pulsa Start
		SpriteFrame(-74, 0, 149, 11, 362, 435) // ¡Toca para Empezar!
		break
	end switch
	
	SpriteFrame(-48,  0, 97, 11, 414, 361) // Press Button (Same sprite regardless of language)
	SpriteFrame(-88, -8, 88,  8, 423, 311) // Sega Copyright text
end sub


// ========================
// Editor Subs
// ========================

sub RSDKDraw
	DrawSprite(0)
end sub


sub RSDKLoad
	LoadSpriteSheet("Title/Title.gif")
	SpriteFrame(-43, 0, 87, 11, 106, 103) // LANG_ENGLISH "Press Start"

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end sub
